//
// Created by REEJI2219 on 2024-09-29.
//
using namespace std;
typedef int ElemType;
class Node {
public:
    ElemType data;
    Node *prior;
    Node *next;
    ~Node() {
        next = nullptr;
        prior=nullptr;
    }
};
class DoubleNodeLiskList {
public:
    Node *head;
    DoubleNodeLiskList() {
        head = new Node();
        head->next = nullptr;
        head->prior = nullptr;
    }

    ~DoubleNodeLiskList() {
        while (head->next!=nullptr) {
            deleteAfterNode(head->next);
        }
        delete head;
    }
    /**
     *
     * 在一个元素之后插入节点
     * @param preNode
     * @param data
     */
    void insetAfterNode(Node *preNode,ElemType data) {
        Node *node = new Node();
        node->data = data;
        //先挂后拆
        node->next = preNode->next;
        if(preNode->next!=nullptr) {
            preNode->next->prior = node;
        }
        node->prior = preNode;
        preNode->next = node;
    }

    /**
     * 尾插法
     * @param data
     */
    void insertLast(ElemType data) {
        Node *current = head;
        while (current->next!=nullptr) {
            current =current->next;
        }
        insetAfterNode(current,data);
    }

    /**
     * 删除自定元素以后的数据
     * @param preNode 前置节点
     */
    bool deleteAfterNode(Node *preNode) {
        if(preNode==nullptr) {
            return false;
        }
        Node *deleteNode = preNode->next;
        if(deleteNode=nullptr) {
            return false;
        }
        preNode->next = deleteNode->next;
        if( deleteNode->next!=nullptr) {
            deleteNode->next->prior = preNode;
        }
        delete deleteNode;
        return true;
    }
};